//
//  WWShopCartDetilController.m
//  Shopping
//
//  Created by 张湾湾 on 16/8/18.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#import "WWShopCartDetilController.h"
#import "UIBarButtonItem+WWBarButton.h"
#import "WWDeliveryAddressView.h"
#import "WWShoppingCarCell.h"
#import "WWDeliveryMarkView.h"
#import "WWShopPayController.h"
#import "WWRightCellModel.h"

#define kTableViewCellID @"ShopCartDetilControllerID"

@interface WWShopCartDetilController () <UITableViewDataSource, UITableViewDelegate>

/** 收获地址视图 **/
@property (nonatomic, strong) WWDeliveryAddressView *deliveryAddressView;
/** 收货备注信息视图 **/
@property (nonatomic, strong) UIView *markView;
@property (nonatomic, strong) UIView *headerView;

/** 商品列表tableView **/
@property (nonatomic, strong) UITableView *goodsTableView;

/** 商品总价label **/
@property (nonatomic, strong) UILabel *totalCostLabel;

@end

@implementation WWShopCartDetilController

//+(void)load{
//    // 测试数据start ----------------
//    WWShopCarManager *shopCarManager = [WWShopCarManager sharedShopCarManager];
//    WWRightCellModel *rightCellModel = [WWRightCellModel new];
//    rightCellModel.id = 10000;
//    rightCellModel.name = @"商品名称";
//    rightCellModel.price = @"18.9";
//    rightCellModel.userBuyCount = 1;
//    [shopCarManager addSupermarketGoodsToShopCar:rightCellModel];
//    
//    WWRightCellModel *rightCellModel2 = [WWRightCellModel new];
//    rightCellModel2.id = 10002;
//    rightCellModel2.name = @"商品名称2";
//    rightCellModel2.price = @"22";
//    rightCellModel2.userBuyCount = 1;
//    
//    [shopCarManager addSupermarketGoodsToShopCar:rightCellModel2];
//    // 测试数据end ----------------
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}


/** 设置界面 **/
-(void)setupUI{
    [self setupNav];
    [self setHeaderInfoView];
    [self setMiddleTableView];
    [self setBottomCalculateView];
}


#pragma mark - 点击"选好了"按钮
-(void)goToPay:(UIButton *)sender{
    NSLog(@"\"选好了\"按钮点击了,%@",self.totalCostLabel.text);
    
    WWShopPayController *payVc = [WWShopPayController new];
    // 跳转到结算支付页面
    [self.navigationController pushViewController:payVc animated:YES];
}


/** 点击导航栏上返回图标 **/
- (void)didClickBlack{
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - tableView dataSource & delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [[WWShopCarManager sharedShopCarManager] getShopCarGoodsList].count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WWShoppingCarCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellID];
    
    if (cell == nil) {
        cell = [[WWShoppingCarCell alloc] init];
    }
    
    NSMutableArray<WWRightCellModel *> *mArray = [[WWShopCarManager sharedShopCarManager] getShopCarGoodsList];
    cell.rightCellModel = mArray[indexPath.row];
    
    __weak typeof(self) weakSelf = self;
    cell.changeTotalCostBlock = ^(CGFloat totalCostDelta){
        // 当前总费用
        CGFloat currentTotalCost = [[WWShopCarManager sharedShopCarManager] getTotalPriceOfShopCarGoods];
        // 修改总费用
        [weakSelf setTotalCostLabelTextWithCost:currentTotalCost];
        // 一旦购物车发生变化,则刷新页面
        [tableView reloadData];
    };
    
    return cell;
}


/** 组头高度 **/
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 260;
    }
    
    return 0;
}


/** 组头view **/
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return self.headerView;
}


/** 滚动tableView **/
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}


/** 设置总价格label的文字显示 **/
-(void)setTotalCostLabelTextWithCost:(CGFloat)cost{
    self.totalCostLabel.text = [NSString stringWithFormat:@"共$ %.2f",cost];
}


#pragma mark - 设置界面
/** 设置底部的tabBar结算视图 **/
-(void)setBottomCalculateView{
    UIView *calculateView = [[UIView alloc] init];
    calculateView.frame = CGRectMake(0, SCREENH - 49, SCREENW, 49);
    calculateView.backgroundColor = [UIColor whiteColor];
    [self.navigationController.view addSubview:calculateView];
    
    // 添加商品总价label
    self.totalCostLabel = [[UILabel alloc] init];
    self.totalCostLabel.textColor = [UIColor redColor];
    [calculateView addSubview:self.totalCostLabel];
    
    // 赋值
    self.totalCostLabel.text = [NSString stringWithFormat:@"共$ %.2f",[[WWShopCarManager sharedShopCarManager] getTotalPriceOfShopCarGoods]];
    
    [self.totalCostLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(0);
        make.leading.equalTo(20);
        make.width.equalTo(150);
    }];
    
    // 添加"选好了"按钮
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(calculateView.frame.size.width - 90, 0, 90, calculateView.frame.size.height)];
    [btn setTitle:@"选好了" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn setBackgroundColor:KColorWithRGB(253, 212, 49)];
    [btn addTarget:self action:@selector(goToPay:) forControlEvents:UIControlEventTouchUpInside];
    [calculateView addSubview:btn];
}


/** 设置中间部分的商品展示tableView **/
-(void)setMiddleTableView{
    self.goodsTableView.dataSource = self;
    self.goodsTableView.delegate = self;
    self.goodsTableView.rowHeight = 52;
    
    self.view = self.goodsTableView;
}


/** 设置组头的收货信息视图 **/
-(void)setHeaderInfoView{
    self.deliveryAddressView.backgroundColor = [UIColor whiteColor];
    self.markView.backgroundColor = [UIColor whiteColor];
    
    [self.headerView addSubview:self.deliveryAddressView];
    [self.headerView addSubview:self.markView];
    
    self.deliveryAddressView.frame = CGRectMake(0, 10, SCREENW, 80);
    self.markView.frame = CGRectMake(0, CGRectGetMaxY(self.deliveryAddressView.frame) + 10, SCREENW, 160);
    
    // UIImageView *bgView = [[UIImageView alloc] initWithImage:@""];
}


/** 设置导航栏 **/
- (void)setupNav{
    self.title = @"购物车";
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImageName:@"v2_goback" target:self action:@selector(didClickBlack)];
}


#pragma mark - 懒加载
/** 商品列表tableView **/
-(UITableView *)goodsTableView{
    if (_goodsTableView == nil) {
        _goodsTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    }
    
    return _goodsTableView;
}


/** 收货地址视图 **/
-(WWDeliveryAddressView *)deliveryAddressView{
    if (_deliveryAddressView == nil) {
        _deliveryAddressView = [[WWDeliveryAddressView alloc] init];
    }
    
    return _deliveryAddressView;
}


/** 收货备注信息视图 **/
-(UIView *)markView{
    if (_markView == nil) {
        _markView = [WWDeliveryMarkView new];
    }
    
    return _markView;
}


/** 收货信息视图 **/
-(UIView *)headerView{
    if (_headerView == nil) {
        _headerView = [UIView new];
    }
    
    return _headerView;
}


- (void)dealloc{
    
}

@end
